﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">配置名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="title" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="config-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

<div class="layui-card">
    <div class="layui-card-body">
        <table id="config-table" lay-filter="config-table"></table>
    </div>
</div>

<script type="text/html" id="config-toolbar">

    @{await Html.RenderPartialAsync("_ToolbarAuthTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "Add", "新增" }, { "Delete", "删除" } }
        });
    }
</script>

<script type="text/html" id="config-bar">
    @{
       await Html.RenderPartialAsync("_OperationTpl", new SystemFunctionModel
        {
            MenuKey = SysMenuConstant.MENU_SYSCONFIGURATION_MANAGE,
            FunDictionary = new Dictionary<string, string> { { "Update", "编辑" }, { "Delete", "删除" } }
        });
    }
</script>
<script type="text/html" id="config-enable">
    <input type="checkbox" value="{{d.id}}" disabled lay-skin="switch" lay-text="启用|禁用" lay-filter="config-enable" {{d.onOff ? 'checked' : '' }}>
</script>
@section scripts{

    <script>
        layui.use(['table', 'form', 'lscom'], function () {
            let table = layui.table;
            let form = layui.form;
            let $ = layui.jquery;
            let lscom = layui.lscom;
            let MODULE_PATH = "/Systems/SysConfiguration/";
            var istoolbar = $.trim($("#config-bar").html())=="" ? true : false;
            let cols = [
                [{
                    type: 'checkbox'
                },
                {
                    title: '名称',
                    field: 'title',
                    align: 'left',
                    width: 200
                },
                {
                    title: '编码',
                    field: 'code',
                    align: 'left',
                    width: 150
                },
                {
                    title: '状态',
                    field: 'onOff',
                    align: 'center',
                    templet: '#config-enable',
                    width: 100
                },
                {
                title: '参数',
                field: 'args',
                align: 'center'
                },
                {
                    title: '备注',
                    field: 'remark',
                    align: 'center',
                    width: 150
                },
                {
                    title: '操作',
                    templet: '#config-bar',
                    align: 'center',
                    width: 150,
                    hide:istoolbar
                }
                ]
            ];

            table.render({
                elem: '#config-table',
                url: MODULE_PATH + "GetList",
                page: true,
                cols: cols,
                skin: 'line',
                height: 'full-148',
                toolbar: '#config-toolbar',
                defaultToolbar: [{
                    title: '刷新',
                    layEvent: 'refresh',
                    icon: 'layui-icon-refresh',
                }, 'filter', 'print', 'exports']
            });

            table.on('tool(config-table)', function (obj) {
                var data = obj.data;
                if (obj.event === 'Delete') {
                    window.remove(data.id);
                } else if (obj.event === 'Update') {
                    window.edit(data.id);
                }
            });

            table.on('toolbar(config-table)', function (obj) {
                if (obj.event === 'Add') {
                    window.add();
                } else if (obj.event === 'refresh') {
                    window.refresh();
                } else if (obj.event === 'Delete') {

                    let checkData = table.checkStatus(obj.config.id).data;
                    if (checkData.length === 0) {
                        parent.layer.msg("未选中数据", {
                            icon: 3,
                            time: 1000
                        });
                        return false;
                    }
                    var ids = checkData.map(function (d) { return d.id; });
                    window.remove(ids.join(','));
                }
            });

            form.on('submit(config-query)', function (data) {
                table.reload('config-table', {
                    where: data.field
                })
                return false;
            });

            window.add = function () {
                parent.layer.open({
                    type: 2,
                    title: '新增配置项',
                    shade: 0.1,
                    area: ['900px', '650px'],
                    content: MODULE_PATH + 'Create',
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }
            window.edit = function (id) {
                parent.layer.open({
                    type: 2,
                    title: '修改配置项',
                    shade: 0.1,
                    area: ['900px', '650px'],
                    content: MODULE_PATH + 'Edit' + lscom.utils.formatString("?id={0}", id),
                    btn: ['确定', '取消'],
                    yes: function (index, layero) {
                        parent.window['layui-layer-iframe' + index].submitForm();
                    }
                });
            }

            window.remove = function (data) {
                parent.layer.confirm('确定删除吗?', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    parent.layer.close(index);
                    let waitIndex = parent.layer.load(2);
                    lscom.ajax({
                        url: MODULE_PATH + 'Delete',
                        data: JSON.stringify({ "ids": data }),
                        lsHandleError: false
                    }).done(function (data) {
                        if (data.code == 0) {
                            lscom.notify.success(data.message);
                            table.reload('config-table');
                        }
                    }).fail(function (message) {
                        lscom.notify.error(message);
                    }).always(function () {
                        parent.layer.close(waitIndex);
                    });
                });
            }
            window.saveCallback = function (data) {
                parent.layer.close(data.index);
                lscom.notify.success(data.message);
                table.reload('config-table');
            }
            window.refresh = function () {
                table.reload('config-table');
            }
        });
    </script>

}
